﻿package idv.cjcat.fenofsm {
	use namespace ffsm;
	
	import flash.events.*;
	import flash.utils.*;
	
	import idv.cjcat.fenofsm.events.*;
	
	/**
	 * A <code>State</code> object represents a state in a finite state machine.
	 * This class is not meant to be instantiated manually.
	 * Use the <code>FSMachine.createState()</code> method instead to create a state.
	 */
	public class State extends EventDispatcher {
		
		/**
		 * The custom data of the state.
		 * <p>
		 * You can assign whatever object you want to this property to get it associated with this state.
		 * </p>
		 */
		public var data:*;
		
		/** @private */
		ffsm var _name:String;
		/** @private */
		ffsm var _outTransitions:Array = new Array();
		/** @private */
		ffsm var _fsm:FSMachine;
		
		public function State() {
			
		}
		
		/**
		 * Initializes a state.
		 * <p>
		 * Do not use this constructor to create a state object.
		 * Use the <code>FSMachine.createState()</code> method instead.
		 * </p>
		 * @param	name     The name of the state.
		 */
		final public function initialize(name:String, fsm:FSMachine):void {
			this._name = name;
			this._fsm = fsm;
		}
		
		/**
		 * The name of the state.
		 */
		public function get name():String { return _name; }
		
		/**
		 * A vector of transitions originating from the state.
		 */
		public function get outTransitions():Array {
			var v:Array = new Array(_outTransitions.length);
			for (var i:int = 0; i < _outTransitions.length; i++) v[i] = _outTransitions[i];
			return v;
		}
		
		/**
		 * The finite state machine the state is in.
		 */
		public function get fsm():FSMachine { return _fsm; }
		
		/**
		 * Override this function to generate custom XML representation.
		 * Altering the <code>name</code> attribute in the root node 
		 * does not affect the final output from the <code>FSMachine.toXML()</code> method.
		 * @param	xml
		 */
		internal function toXML():XML { return <state/> }
		
		/**
		 * Override this function to perform custom XML parsing.
		 * @param	xml
		 */
		internal function parseXML(xml:XML):void { }
		
		override public function toString():String {
			return "[State " + "name='" + _name + "']";
		}
	}
}